define(['app','js/directive/listTree','js/filter/tree2Arr'], function( app ) {
'use strict';
/**
 * 部门管理
 *
 */
app.controller('Sector', function ($scope, dataFactoryCRM, $q, $compile, $timeout) {
	$scope.treeSectors;
    $scope.users ;
    $scope.sector_now = {};
    $scope.edit_data= {manager_id:0,sector_id:0,parent_sector:0,info:'',sector_name:''};

    $scope.getSectors = function(){
        dataFactoryCRM.getDataByAPI('Sector/index').then(
            function( d ){
                $scope.treeSectors = d.data;
                $scope.users = d.users;
                $scope.sector_now = d.sector;
            }
        );
    }

     $scope.nodeAdd = function(){
        $scope.add_data = {sector_name:'',info:'',parent_sector:'' };
        $scope.add_data.parent_sector = $scope.sector_now.sector_id;
        $timeout(function(){
            $('.ui.add_sector').dropdown('refresh');
            $('.ui.add_sector').dropdown('set selected',  $scope.sector_now.sector_id );
        });
    
        $('.ui.sector.add.modal').modal({
            onApprove: function() {
                if(  $('#frm-add').form('is valid') ){
                    dataFactoryCRM.postDataByAPI('sector/add', $scope.add_data)
                      .then(function(d) {
                        if (d.rcode) {
                            $scope.getSectors();
                            return true;
                        } else {
                          alert(d.msg);
                          return false;
                        }
                      });
                      return true;
                }
                return false;
            }
        }).modal('show');
    }

    $scope.nodeEdit = function( _dnode ){
        $scope.edit_data.sector_id = _dnode.sector_id;
        $scope.edit_data.sector_name = _dnode.sector_name;
        $scope.edit_data.info = _dnode.info;
        $scope.edit_data.parent_sector = _dnode.parent_sector;
        $scope.edit_data.manager_id = _dnode.manager_id;
        $timeout(function(){
            $('.ui.edit_sector').dropdown('refresh');
            $('.ui.edit_sector:eq(0)').dropdown('set selected',  _dnode.parent_sector );
            $('.ui.edit_sector:eq(1)').dropdown('set selected',  _dnode.manager_id );
        });
        
        $('.ui.sector.edit').modal({
            onApprove: function() {
                if(  $('#frm-edit').form('is valid') ){
                    dataFactoryCRM.postDataByAPI('sector/edit', $scope.edit_data )
                      .then(function(d) {
                        if (d.rcode) {
                            $scope.getSectors();
                            return true;
                        }
                        else {
                          alert(d.msg);
                          return false;
                        }
                      });
                    return true;
                  }
            }
        }).modal('show');
    }

	$scope.sector_del = function( sector_id ){
	    var _d = {id:0};
        _d.id = sector_id;
	  dataFactoryCRM.postDataByAPI('Sector/delete', _d ).then(
			function( d ){
				if(  d.rcode ){
                    $scope.getSectors();
                    return true;
                }
				else{
                    alert( d.msg );
                }
			}
		);
	}

    $scope.getSectors();
});

});